package com.itheima.api.object;

import java.util.Objects;

public class ObjectDemo {
    public static void main(String[] args) {
        Student student = new Student("张三",21);
        Student student2 = new Student("张三",21);
        //public static String toString()   返回该对象的字符串表示
        System.out.println("student.toString() = " + student.toString());

        //public static boolean equals(Object obj) 指示其他某个对象是否与此对象“相等”。
        System.out.println("student.equals(student2) = " + student.equals(student2));


    }
}

class Student{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {   //多态    将Student对象向上转型成Object对象
        if (this == o) return true;     //this代表方法的调用者对象
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;  //多态    将Object对象向下转型回Student对象
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        System.out.println("super.toString() = " + super.toString());
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
